package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.pojo.RecommendUser;
import com.tanhua.dubbo.pojo.UserInfo;
import com.tanhua.dubbo.vo.PageInfo;
import com.tanhua.server.config.DefaultConfig;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TodayBestService {

    @Autowired
    private UserService userService;

    @DubboReference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;



    @Autowired
    private DefaultConfig defaultConfig;


    public Object queryTodayBest() {
        Long userId = UserThreadLocal.get();

        //查询得分最高的人
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);
        if (null == recommendUser) {
            //给出一个默认的推荐用户
            recommendUser = new RecommendUser();
            recommendUser.setUserId(this.defaultConfig.getUser());
            recommendUser.setToUserId(userId);
            recommendUser.setScore(RandomUtil.randomDouble(80, 99));
        }

        //查询用户信息，封装成TodayBest对象返回
        UserInfo userInfo = this.userInfoApi.queryByUserId(recommendUser.getUserId());

        TodayBest todayBest = BeanUtil.toBeanIgnoreError(userInfo, TodayBest.class);
        todayBest.setFateValue(recommendUser.getScore().longValue());
        todayBest.setGender(userInfo.getSex().name().toLowerCase());

        return todayBest;
    }

    /**
     * 查询推荐用户列表
     *
     * @param queryParam
     * @return
     */
    public Object queryRecommendation(RecommendUserQueryParam queryParam) {
        Long userId = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());

        //通过dubbo服务查询推荐用户列表
        PageInfo<RecommendUser> pageInfo = this.recommendUserApi.queryPageInfo(userId, queryParam.getPage(), queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            records = new ArrayList<>();
            //给出默认的推荐用户
            for (Long uid : this.defaultConfig.getUserList()) {
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setUserId(uid);
                recommendUser.setToUserId(userId);
                recommendUser.setScore(RandomUtil.randomDouble(80, 99));
                records.add(recommendUser);
            }
        }

        //查询用户信息列表
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        List<TodayBest> todayBestList = new ArrayList<>();

        //最终方案
        Map<Long, UserInfo> userInfoMap = this.userInfoApi.queryMapByUserIdList(userIdList);
        for (RecommendUser record : records) {
            UserInfo userInfo = userInfoMap.get(record.getUserId());
            TodayBest todayBest = BeanUtil.toBeanIgnoreError(userInfo, TodayBest.class);
            todayBest.setFateValue(record.getScore().longValue()); //缘分值
            //根据性别筛选,同性相斥
            UserInfo userInfo1 = this.userInfoApi.queryByUserId(userId);
            if(userInfo1.getSex().getValue() == 1){
                if(userInfo.getSex().getValue() != 2){
                    userInfoMap.remove(record.getUserId());
                }
            }else{
                if(userInfo1.getSex().getValue() == 2){
                    if(userInfo.getSex().getValue() != 1){
                        userInfoMap.remove(record.getUserId());
                    }
                }
            }

            todayBest.setGender(userInfo.getSex().name().toLowerCase()); //性别
            todayBestList.add(todayBest);
        }

        // List<UserInfo> userInfoList = this.userInfoApi.queryByUserIdList(userIdList);
        //实现一：无法保证顺序，需要在最后进行排序
        // for (UserInfo userInfo : userInfoList) {
        //     TodayBest todayBest = BeanUtil.toBeanIgnoreError(userInfo, TodayBest.class);
        //     todayBest.setGender(userInfo.getSex().name().toLowerCase());
        //     for (RecommendUser record : records) {
        //         if (ObjectUtil.equal(userInfo.getUserId(), record.getUserId())) {
        //             todayBest.setFateValue(record.getScore().longValue());
        //             break;
        //         }
        //     }
        //     todayBestList.add(todayBest);
        // }
        //


        //实现二：可以保证顺序，无法实现用户的筛选
        // for (RecommendUser record : records) {
        //     TodayBest todayBest = new TodayBest();
        //     todayBest.setFateValue(record.getScore().longValue());
        //     for (UserInfo userInfo : userInfoList) {
        //         if (ObjectUtil.equal(userInfo.getUserId(), record.getUserId())) {
        //             BeanUtil.copyProperties(userInfo, todayBest);
        //             todayBest.setGender(userInfo.getSex().name().toLowerCase());
        //             break;
        //         }
        //     }
        //     todayBestList.add(todayBest);
        // }

        pageResult.setItems(todayBestList);
        return pageResult;
    }
}
